package com.gzdh.dishrecognition.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.entity.MacMainpSbruanDinnerEntity;
import com.gzdh.dishrecognition.entity.OrdersEntity;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class UnOnLineOrderAdapter extends RecyclerView.Adapter<UnOnLineOrderAdapter.ViewHolder> {

    private List<OrdersEntity> list;

    public UnOnLineOrderAdapter(Context context) {
        this.list = new ArrayList<>();
    }


    public void setList(List<OrdersEntity> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public String getTotalPrice() {
        BigDecimal total = new BigDecimal(0);
        for (OrdersEntity ordersEntity : this.list) {
            total = total.add(new BigDecimal(ordersEntity.getAmountPrice()));
        }
        return total.setScale(2, RoundingMode.DOWN).toString();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_unonline, parent, false);
        return new ViewHolder(itemView);
    }

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.orderTv.setText(String.valueOf(position + 1));
        holder.nameTv.setText(list.get(position).getName().trim());
        holder.amountTv.setText(list.get(position).getAmountPrice());
        holder.createTimeTv.setText(simpleDateFormat.format(list.get(position).getCreateTime()));
        if (position % 2 == 0) {
            holder.mainLinear.setBackgroundColor(Color.rgb(255, 255, 255));
        } else {
            holder.mainLinear.setBackgroundColor(Color.rgb(200, 200, 200));
        }
        if(list.get(position).getType() == 0) {
            holder.scTv.setText("自购支付");
        } else {
            holder.scTv.setText("补贴支付");
        }
        holder.stateTv.setText(list.get(position).getUpLoad() ? "已上传" : "未上传");
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mainLinear;
        TextView orderTv;
        TextView nameTv;
        TextView amountTv;
        TextView createTimeTv;
        TextView scTv;
        TextView stateTv;

        ViewHolder(View item) {
            super(item);
            mainLinear = item.findViewById(R.id.mainLinear);
            orderTv = item.findViewById(R.id.orderTv);

            nameTv = item.findViewById(R.id.nameTv);
            amountTv = item.findViewById(R.id.amountTv);
            createTimeTv = item.findViewById(R.id.createTimeTv);
            scTv = item.findViewById(R.id.scTv);
            stateTv = item.findViewById(R.id.stateTv);
        }

    }


}
